package com.sz.app.oss.api.logistics.base;

import com.sz.app.oss.constants.MessagesCodes;
import com.sz.app.oss.constants.Permissions;
import com.sz.biz.app.web.BaseController;
import com.sz.biz.logistics.base.entity.RegionSchema;
import com.sz.biz.logistics.base.service.RegionSchemaService;
import com.sz.biz.logistics.base.dto.RegionSchemaDto;
import com.sz.common.base.dto.QueryParams;
import com.sz.common.base.dto.ResultDto;
import com.sz.common.base.utils.EntityUtils;
import com.sz.common.base.utils.StringUtils;
import io.swagger.annotations.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * Function: 分区策略 controller <br>
 * Author: hpeng.wang <br>
 * Date: 2017-02-15 15:08:00
 */

@RestController
@RequestMapping("${api}/biz/partition/schema")
@Api(description = " ", tags = "030-001、分区-方案")
public class RegionSchemaController extends BaseController {
    @Autowired
    private RegionSchemaService service;


    /**
     * 新增.
     */
    @ApiOperation(value = "新增", notes = "新增分区方案数据<br>权限：" + Permissions.BIZ_MGT_BASE_REGION)
    @RequestMapping(value = "", method = {RequestMethod.POST})
    @RequiresPermissions(Permissions.BIZ_MGT_BASE_REGION)
    public ResultDto save(@RequestBody @ApiParam(value = "要保存的数据", required = true) @Validated RegionSchemaDto dto) {
        RegionSchema entry = new RegionSchema();
        dto.setId(null);// 保存时忽略id
        EntityUtils.copyPropertiesIgnoreNull(dto, entry);
        String uid = service.save(entry);
        ResultDto result = getSaveSuccessDto(MessagesCodes.MODULE_REGION_SCHEMA);
        result.setData(uid);
        return result;

    }

    /**
     * 批量删除
     *
     * @param ids 要删除的id集合
     * @return 操作结果
     */
    @ApiOperation(value = "删除", notes = "根据指定id集合删除数据<br>权限：" + Permissions.BIZ_MGT_BASE_REGION, httpMethod = "POST")
    @RequestMapping(value = "/deletions", method = {RequestMethod.POST})
    @RequiresPermissions(Permissions.BIZ_MGT_BASE_REGION)
    public ResultDto batchDelete(@RequestBody @ApiParam(value = "要删除的id集合", required = true) List<Integer> ids) {
        service.batchdelete(ids);
        ResultDto dto = getDeleteSuccessDto(MessagesCodes.MODULE_REGION_SCHEMA);
        return dto;
    }

    /**
     * 修改.
     */
    @RequestMapping(value = "{id}", method = {RequestMethod.POST})
    @ApiOperation(value = "修改", notes = "修改分区方案数据<br>权限：" + Permissions.BIZ_MGT_BASE_REGION)
    @Transactional(propagation = Propagation.REQUIRED)
    @RequiresPermissions(Permissions.BIZ_MGT_BASE_REGION)
    public ResultDto update(@PathVariable int id, @RequestBody RegionSchemaDto dto) {
        int count = 0;
        RegionSchema entry = service.findById(id);
        if (entry != null) {
            dto.setId(id);
            EntityUtils.copyPropertiesIgnoreNull(dto, entry);
            entry.setLastUpdateTime(new Date());
            count = service.update(entry);
        }
        ResultDto result = getUpdateSuccessDto(MessagesCodes.MODULE_REGION_SCHEMA);
        result.setData(count);
        return result;
    }

    /**
     * 根据id查询数据
     *
     * @param id 分区方案的id
     * @return 查询到的单条分区方案
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @ApiOperation(value = "查询_Id", notes = "根据id获取到的分区方案")
    public ResultDto findById(@ApiParam(value = "分区方案id", required = true) @PathVariable Integer id) {
        RegionSchemaDto dto = new RegionSchemaDto();
        RegionSchema entry = service.findById(id);
        if (entry != null) {
            EntityUtils.copyPropertiesIgnoreNull(entry, dto);
        }
        ResultDto result = new ResultDto();
        result.setData(dto);
        return result;
    }

    /**
     * 根据条件获取数据集合
     *
     * @param q         参数信息
     * @param pageIndex 哪页
     * @param pageSize  分页大小
     * @param sort      排序字段的名称
     * @param type      类型
     * @param asc       true asc false desc
     * @return 查询的数据集合
     */
    @ApiOperation(value = "查询", notes = "根据指定条件获取数据集合")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "q", value = "模糊查询参数", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "pageIndex", value = "分页", dataType = "int", paramType = "query", defaultValue
                    = "1"),
            @ApiImplicitParam(name = "pageSize", value = "每页条数,max<2000", dataType = "int", paramType = "query",
                    defaultValue = "10"),
            @ApiImplicitParam(name = "sort", value = "排序的字段", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "type", value = "类型", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "asc", value = "排序方式", dataType = "boolean", paramType = "query", defaultValue =
                    "true")
    })

    @RequestMapping(value = "", method = RequestMethod.GET)
    public ResultDto find(@RequestParam(required = false) String q,
                          @RequestParam(required = false, defaultValue = "1") int pageIndex,
                          @RequestParam(required = false, defaultValue = "10") int pageSize,
                          @RequestParam(required = false) String sort,
                          @RequestParam(required = false) String type,
                          @RequestParam(required = false, defaultValue = "true") Boolean asc
    ) {
        q = StringUtils.replaceSqlPattern(q);
        ResultDto result = service.findByParam(new QueryParams(pageSize, pageIndex, q, sort, asc).put("type", type));
        return result;
    }

    /**
     * 根据name获取数据集合
     *
     * @param name 参数信息
     * @return 查询的数据集合
     */
    @ApiOperation(value = "查询", notes = "根据name获取数据集合")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "查询参数", dataType = "string", paramType = "query")
    })

    @RequestMapping(value = "/name", method = RequestMethod.GET)
    public ResultDto findByName(@RequestParam(required = false) String name) {
        name = StringUtils.replaceSqlPattern(name);
        ResultDto result = service.findByName(name);
        return result;
    }

    /**
     * 异步校验分区方案的code
     *
     * @param code 分区方案code
     * @param id   　分区方案id，修改时传id，新增时为NULL
     * @return　校验的结果
     */
    @ApiOperation(value = "异步校验分区方案编码", notes = "异步校验分区方案编码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", value = "分区方案编码", required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "id", value = "分区方案id 修改时传id 新增时传空", dataType = "int", paramType = "query")})
    @RequestMapping(value = "/partition/schema/code", method = {RequestMethod.GET})
    public ResultDto partitionSchemaCheckCode(@RequestParam String code,
                                              @RequestParam(required = false, defaultValue = "0") int id) {
        service.regionSchemaCheckCode(id, code);
        return new ResultDto();
    }

}
